package cn.edu.neusoft.yuejian12.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian12.Activity.TrainInfoActivity;
import cn.edu.neusoft.yuejian12.JavaBean.TrainInfo;
import cn.edu.neusoft.yuejian12.R;

/**
 * Created by 木子李 on 2017/10/10.
 */

public class TrainListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context mContext;
        private List mDataList;
        private LayoutInflater mLayoutInflater;
        public TrainListAdapter(Context mContext, List mDataList) {
            this.mContext = mContext;
            this.mDataList = mDataList;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_trainlist, parent, false);
            return new ViewHolder(v);

        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final TrainInfo entity = (TrainInfo) mDataList.get(position);
            if (null == entity)
                return;
            TrainListAdapter.ViewHolder viewHolder = (TrainListAdapter.ViewHolder) holder;
            viewHolder.train_name.setText(entity.getName());
            String address="http://172.24.10.175/workout/Uploads/"+entity.getThumb();
           /* if(!entity.getThumb().equals("")){
                Picasso.with(mContext).load(address).into(viewHolder.train_thumb);
            }*/
            if(!entity.getThumb().isEmpty()) {
                Picasso.with(mContext).load(address).into(viewHolder.train_thumb);
            }else{
                viewHolder.train_thumb.setImageResource(R.drawable.no_pic);
            }
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext,TrainInfoActivity.class);
                    intent.putExtra("train_id",entity.getId());
                    mContext.startActivity(intent);
                }
            });






       /* viewHolder.bar.setNumStars(5);
        viewHolder.bar.setRating((int)entity.get("ratingBar2"));
        viewHolder.news_thumb.setImageResource(Integer.parseInt(entity.get("news_thumb").toString()));*/


        }

        @Override
        public int getItemCount() {
            return mDataList.size();
        }
        public class ViewHolder extends RecyclerView.ViewHolder{
            ImageView train_thumb;
            TextView train_name;
            LinearLayout train_container;



            public ViewHolder(View itemView) {
                super(itemView);
                train_container = (LinearLayout) itemView.findViewById(R.id.train_container);
                train_name = (TextView) itemView.findViewById(R.id.train_name);
                train_thumb = (ImageView) itemView.findViewById(R.id.train_thumb);


            }
        }

    }



